<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../">
  <title data-ice="title">utils/status.js | API</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<link rel="stylesheet" href="./inject/css/0-vue.css"><link rel="stylesheet" href="./inject/css/0-app.css"><link rel="stylesheet" href="./inject/css/0-esdoc.css"><script src="./inject/script/0-custom.js"></script><meta name="description" content="&#x4F01;&#x4E1A;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x5E73;&#x53F0;"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="API"><meta property="twitter:description" content="&#x4F01;&#x4E1A;&#x5373;&#x65F6;&#x901A;&#x8BAF;&#x5E73;&#x53F0;"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/easysoft/xuanxuan.git"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/color.js~Color.html">Color</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/delay-action.js~DelayAction.html">DelayAction</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/status.js~Status.html">Status</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/status.js~StatusKeeper.html">StatusKeeper</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/utils/task-queue.js~TaskQueue.html">TaskQueue</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createDate">createDate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createPhpTimestramp">createPhpTimestramp</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatDate">formatDate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatSpan">formatSpan</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getTimeBeforeDesc">getTimeBeforeDesc</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameDay">isSameDay</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameMonth">isSameMonth</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isSameYear">isSameYear</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isToday">isToday</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isYestoday">isYestoday</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-classes">classes</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-escape">escape</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSearchParam">getSearchParam</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isWebUrl">isWebUrl</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-linkify">linkify</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-rem">rem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-strip">strip</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-cutImage">cutImage</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getImageInfo">getImageInfo</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-limittimepromise">limittimepromise</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-mdifileicon">mdifileicon</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-pinyin">pinyin</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-plain">plain</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-matchScore">matchScore</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatKeyDecoration">formatKeyDecoration</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getKeyDecoration">getKeyDecoration</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isOnlyModifyKeys">isOnlyModifyKeys</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getCodeFromString">getCodeFromString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-longShadow">longShadow</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-skinStyle">skinStyle</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-sortList">sortList</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-clearStore">clearStore</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getStoreItem">getStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getStoreLength">getStoreLength</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-removeStoreItem">removeStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-setStoreItem">setStoreItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-storeForEach">storeForEach</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-storeGetAll">storeGetAll</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatBytes">formatBytes</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formatString">formatString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-ifEmptyStringThen">ifEmptyStringThen</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isEmptyString">isEmptyString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isNotEmptyString">isNotEmptyString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-timesequence">timesequence</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TIME_DAY">TIME_DAY</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-renderer">renderer</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-fileIcons">fileIcons</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BYTE_UNITS">BYTE_UNITS</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">utils/status.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">/**
 * &#x72B6;&#x6001;&#x5B58;&#x50A8;&#x7C7B;
 *
 * @class StatusKeeper
 */
export class StatusKeeper {
    /**
     * &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x5B58;&#x50A8;&#x7C7B;&#x5B9E;&#x4F8B;
     * @param {number|string} status &#x5F53;&#x524D;&#x72B6;&#x6001;
     * @param {Status} mapper &#x72B6;&#x6001;&#x8868;&#x5BF9;&#x8C61;
     * @constructor
     */
    constructor(status, mapper) {
        this.mapper = mapper;
        this.status = mapper.getValue(status);
        if (this.status === undefined) {
            this.status = mapper.defaultValue;
        }
    }

    /**
     * &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x540D;&#x79F0;
     * @type {string}
     * @readonly
     * @memberof StatusKeeper
     * @instance
     */
    get name() {
        return this.mapper.getName(this.status);
    }

    /**
     * &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x503C;
     * @type {number}
     * @readonly
     * @memberof StatusKeeper
     * @instance
     */
    get value() {
        return this.mapper.getValue(this.status);
    }

    /**
     * &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x53D8;&#x66F4;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @type {Function}
     * @memberof StatusKeeper
     * @instance
     */
    get onChange() {
        return this._onChange;
    }

    /**
     * &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x53D8;&#x66F4;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @param {Function} callback &#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @memberof StatusKeeper
     * @instance
     */
    set onChange(callback) {
        this._onChange = callback;
    }

    /**
     * &#x83B7;&#x53D6;&#x68C0;&#x67E5;&#x72B6;&#x6001;&#x662F;&#x5426;&#x5141;&#x8BB8;&#x53D8;&#x66F4;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @type {Function}
     * @memberof StatusKeeper
     * @instance
     */
    get canChange() {
        return this._canChange;
    }

    /**
     * &#x8BBE;&#x7F6E;&#x68C0;&#x67E5;&#x72B6;&#x6001;&#x662F;&#x5426;&#x5141;&#x8BB8;&#x53D8;&#x66F4;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @param {Function} callback &#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;
     * @memberof StatusKeeper
     * @instance
     */
    set canChange(callback) {
        this._canChange = callback;
    }

    /**
     * &#x53D8;&#x66F4;&#x72B6;&#x6001;
     *
     * @param {string|number} nameOrValue &#x65B0;&#x7684;&#x72B6;&#x6001;&#x503C;&#x6216;&#x540D;&#x79F0;
     * @memberof StatusKeeper
     * @return {void}
     * @instance
     */
    change(nameOrValue) {
        const value = this.mapper.getValue(nameOrValue);
        const oldValue = this.value;
        if (value !== undefined &amp;&amp; oldValue !== value) {
            if (!this._canChange || this._canChange(value, oldValue)) {
                this.status = value;
                if (typeof this._onChange === &apos;function&apos;) {
                    this._onChange(value, oldValue, this);
                }
            } else if (DEBUG) {
                console.error(`Status &apos;${oldValue}&apos; cannot change to ${nameOrValue} with the rule.`);
            }
        }
    }

    /**
     * &#x68C0;&#x67E5;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x662F;&#x5426;&#x4E3A;&#x7ED9;&#x5B9A;&#x7684;&#x72B6;&#x6001;
     * @param {string|number} nameOrValue
     * @return {boolean}
     * @memberof StatusKeeper
     * @instance
     */
    is(nameOrValue) {
        const value = this.mapper.getValue(nameOrValue);
        return value !== undefined &amp;&amp; value === this.status;
    }
}

/**
 * &#x72B6;&#x6001;&#x7BA1;&#x7406;&#x7C7B;&#xFF08;&#x72B6;&#x6001;&#x8868;&#xFF09;
 *
 * @class Status
 */
export default class Status {
    /**
     * &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x7C7B;
     * @param {Object.&lt;string, number&gt;} statuses &#x72B6;&#x6001;&#x8868;&#x5BF9;&#x8C61;
     * @param {string|number} defaultStatus &#x9ED8;&#x8BA4;&#x72B6;&#x6001;
     * @constructor
     */
    constructor(statuses, defaultStatus) {
        /**
         * &#x6309;&#x72B6;&#x6001;&#x503C;&#x987A;&#x5E8F;&#x4F9D;&#x6B21;&#x5B58;&#x50A8;&#x72B6;&#x6001;&#x540D;&#x79F0;
         * @type {Object}
         * @private
         */
        this.$values = {};

        Object.keys(statuses).forEach(name =&gt; {
            if (typeof this[name] !== &apos;undefined&apos;) {
                throw new Error(`Cannot create status object, the name &apos;${name}&apos; is not a valid status name.`);
            }
            const value = statuses[name];
            if (typeof value !== &apos;number&apos;) {
                throw new Error(`Cannot create status object, the status value(${value}) must be a number.`);
            }
            this.$values[value] = name;
            this[name] = value;
        });

        if (defaultStatus !== undefined) {
            /**
             * &#x9ED8;&#x8BA4;&#x72B6;&#x6001;
             * @type {number}
             */
            this.defaultStatus = this.getValue(defaultStatus);
        }
        if (this.defaultStatus === undefined) {
            // eslint-disable-next-line prefer-destructuring
            this.defaultStatus = this.values[0];
        }
    }

    /**
     * &#x83B7;&#x53D6;&#x6240;&#x6709;&#x72B6;&#x6001;&#x540D;&#x79F0;
     * @type {Array.&lt;string&gt;}
     * @readonly
     * @memberof Status
     * @instance
     */
    get names() {
        return Object.values(this.$values);
    }

    /**
     * &#x83B7;&#x53D6;&#x6240;&#x6709;&#x72B6;&#x6001;&#x503C;
     * @type {Array.&lt;number&gt;}
     * @readonly
     * @memberof Status
     * @instance
     */
    get values() {
        return Object.keys(this.$values);
    }

    /**
     * &#x83B7;&#x53D6;&#x9ED8;&#x8BA4;&#x72B6;&#x6001;&#x540D;&#x79F0;
     * @type {string}
     * @readonly
     * @memberof Status
     * @instance
     */
    get defaultName() {
        return this.getName(this.defaultStatus);
    }

    /**
     * &#x83B7;&#x53D6;&#x9ED8;&#x8BA4;&#x72B6;&#x6001;&#x503C;
     * @type {number}
     * @readonly
     * @memberof Status
     * @instance
     */
    get defaultValue() {
        return this.getValue(this.defaultStatus);
    }

    /**
     * &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x72B6;&#x6001;&#x7684;&#x540D;&#x79F0;
     * @param {string|number} valueOrName &#x72B6;&#x6001;&#x503C;&#x6216;&#x540D;&#x79F0;
     * @param {string} defaultName &#x9ED8;&#x8BA4;&#x72B6;&#x6001;&#x540D;&#x79F0;
     * @return {string}
     * @memberof Status
     * @instance
     */
    getName(valueOrName, defaultName) {
        let name;
        if (typeof valueOrName === &apos;number&apos;) {
            name = this.$values[valueOrName];
        } else if (this[valueOrName] !== undefined) {
            name = valueOrName;
        }
        return name === undefined ? defaultName : name;
    }

    /**
     * &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x72B6;&#x6001;&#x7684;&#x503C;
     * @param {string|number} valueOrName &#x72B6;&#x6001;&#x503C;&#x6216;&#x503C;
     * @param {number} defaultName &#x9ED8;&#x8BA4;&#x72B6;&#x6001;&#x503C;
     * @return {number}
     * @memberof Status
     * @instance
     */
    getValue(valueOrName, defaultValue) {
        let value;
        if (typeof valueOrName === &apos;string&apos;) {
            value = this[valueOrName];
        } else if (this.$values[valueOrName] !== undefined) {
            value = valueOrName;
        }
        return value === undefined ? defaultValue : value;
    }

    /**
     * &#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x72B6;&#x6001;&#x662F;&#x5426;&#x76F8;&#x540C;
     * @param {string|number} status1 &#x72B6;&#x6001;1
     * @param {string|number} status2 &#x72B6;&#x6001;2
     * @return {boolean}
     * @memberof Status
     * @instance
     */
    isSame(status1, status2) {
        return this.getValue(status1) === this.getValue(status2);
    }

    /**
     * &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x5B58;&#x50A8;&#x7C7B;&#x5B9E;&#x4F8B;
     *
     * @param {string|number} status &#x72B6;&#x6001;&#x503C;&#x6216;&#x540D;&#x79F0;
     * @returns {StatusKeeper}
     * @memberof Status
     * @instance
     */
    create(status) {
        if (status === undefined) {
            status = this.defaultValue;
        }
        return new StatusKeeper(status, this);
    }

    /**
     * &#x72B6;&#x6001;&#x5B58;&#x50A8;&#x7C7B;
     *
     * @constructor StatusKeeper
     * @static
     * @memberof Status
     */
    static Keeper = StatusKeeper;
}
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
